package com.suncaper.shop.controller;

import com.suncaper.shop.domain.Product;
import com.suncaper.shop.service.CategoryService;
import com.suncaper.shop.service.CommentService;
import com.suncaper.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class ProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CommentService commentService;


    @RequestMapping(value = {"/index", "/index.html", "/"})
    public String home(Model model, HttpSession session) {

        session.setAttribute("cart", null);
        session.setAttribute("items", null);
        //第一行商品的Content
        productService.createViewToIndex(1, 3, model);
        model.addAttribute("Hot1", productService.findByHot(1));

        productService.createViewToIndex(21, 3, model);
        model.addAttribute("Hot2", productService.findByHot(20));

        productService.createViewToIndex(41, 3, model);
        model.addAttribute("Hot3", productService.findByHot(41));
        //test
        model.addAttribute("dealsbar", productService.findBydeals()).
                addAttribute("Catebar1", categoryService.findCategory(1, 9).subList(0, 7))
                .addAttribute("Catebar2", categoryService.findCategory(21, 30).subList(0, 7))
                .addAttribute("Catebar3", categoryService.findCategory(41, 50).subList(0, 7));


        return "index";
    }

    /**
     * 进入商品详情页
     *
     * @param pid
     * @param model
     * @return
     */


    @RequestMapping("/simpleproduct/{id}")
    public String goToDetail(@PathVariable("id") Integer pid, Model model, HttpSession session) {

        Product byPid = productService.findByPid(pid);
        List<Product> similar = productService.findSimilar(byPid.getCid());
        model.addAttribute("similar", similar);
        model.addAttribute("detail", byPid);
        productService.updateHot(pid);
        if (commentService.getComments(pid).size() != 0) {
            session.setAttribute("comments", commentService.getComments(pid));
        } else {
            session.removeAttribute("comments");
        }


        return "simple_product";
    }


    @RequestMapping("/deals")
    public String deals(@RequestParam(value = "page", required = false) Integer number,
                        @RequestParam(value = "limit", required = false) Integer limit,
                        Model model, HttpSession session
    ) {
        if (number == null) {
            model.addAttribute("deals", productService.findByPage(1, session, limit));
        } else {
            model.addAttribute("deals", productService.findByPage(number, session, limit));
        }
        return "deals";
    }


}
